package com.hjl.mybatis.servlet; /**
 * @author ServivE
 * @className ${NAME}
 * @description ${description}
 * @version 0.1
 * @date 2024/7/31 19:20
 * @since jdk11
 */

import com.alibaba.fastjson.JSON;
import com.hjl.mybatis.entity.Emp;
import com.hjl.mybatis.mapper.EmpMapper;
import com.hjl.mybatis.utils.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "EmpInfoServlet", value = "/empInfo")
public class EmpInfoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> empInfos = empMapper.getEmpInfo();

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println(JSON.toJSONString(empInfos));
    }
}
